﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System;
/// <summary>
/// 卡牌的UI显示
/// </summary>
public class CardUI : ResumeObject,IPointerClickHandler
{
    /// <summary>
    /// 牌的显示UI
    /// </summary>
    public Image image;
    private Card _card;
    private bool isSelected = false;

    /// <summary>
    /// 卡牌的信息
    /// </summary>
    public Card _Card
    {
        get { return _card; }
        set { _card = value;SetImage(); }
    }

    /// <summary>
    /// 设置卡牌显示的图片
    /// </summary>
    private void SetImage()
    {
        //如果该牌是属于玩家和桌面
        if (_card.BelongTo == CharacterType.Player || _card.BelongTo == CharacterType.Desk)
        {
            Sprite sprite = Resources.Load<Sprite>("Pokers/" + _card.CardName);
            image.sprite = sprite;
        }
        else
        {
            Sprite sprite = Resources.Load<Sprite>("Pokers/CardBack");
            image.sprite = sprite;
        }
    }

    /// <summary>
    /// 点击卡牌
    /// </summary>
    /// <param name="eventData"></param>
    public void OnPointerClick(PointerEventData eventData)
    {       
            if (eventData.button == PointerEventData.InputButton.Left)
            {
                if (_card.BelongTo == CharacterType.Player)
                    IsSelected = !IsSelected;
            }
    }

    /// <summary>
    /// 是否被选中
    /// </summary>
    public bool IsSelected
    {
        get{ return isSelected; }
        set
        {
            //重复选择就不做操作
            if (isSelected == value)
                return;
            if (value == true)
            {
                //如果被选中 就向上移动10
                transform.localPosition += Vector3.up * 10;
            }
            else
            {
                //没有被选中
                transform.localPosition -= Vector3.up * 10;
            }
            isSelected = value;
        }
    }
    /// <summary>
    /// 销毁卡牌调用对象池隐藏对象
    /// </summary>
    public void Destroy()
    {
        PoolManager._Instance.HideObject(gameObject);
    }

    /// <summary>
    /// 显示前设置
    /// </summary>
    public override void BeforeGetObject()
    {
        image = GetComponent<Image>();
    }

    /// <summary>
    /// 隐藏前设置
    /// </summary>
    public override void BeforeHideObject()
    {
        isSelected = false;
        image.sprite = null;
        _card = null;
    }

    /// <summary>
    /// 设置该卡牌的位置在那个父物体下
    /// </summary>
    /// <param name="parent">父物体</param>
    /// <param name="index">父物体下的索引</param>
    public void SetPosition(Transform parent,int index)
    {
        transform.SetParent(parent, false);
        transform.SetSiblingIndex(index);
        //向右方向
        if (_Card.BelongTo == CharacterType.Player || _Card.BelongTo == CharacterType.Desk)
        {
            transform.localPosition = Vector3.right * 25 * index;
            if (IsSelected)
            {
                transform.localPosition += Vector3.up * 10;//在原来的基础上上移
            }
        }
        //向下方向
        else if (_Card.BelongTo == CharacterType.ComputerLeft || _Card.BelongTo == CharacterType.ComputerRight)
        {
            transform.localPosition = -Vector3.up * 15 * index;
        }
    }
}
